﻿Public Class frmComprasGastosListado

    Private Sub frmComprasListado_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        fechaInicio.Value = DateTime.Now.AddDays(-30)
        fechaFin.Value = DateTime.Now
        dgCompras.AutoGenerateColumns = False
        cboTipo.SelectedIndex = 0
        CargarCompras()

    End Sub

    Dim controller As New ControllerSigeco.controller_compras()
    Dim controllerVta As New ControllerSigeco.controller_ventas()

    Private _codCliente As Integer = 0

    Public Property codCliente() As Integer
        Get
            Return _codCliente
        End Get
        Set(ByVal value As Integer)
            _codCliente = value
        End Set
    End Property

    Private _codProveedor As Integer = 0

    Public Property codProveedor() As Integer
        Get
            Return _codProveedor
        End Get
        Set(ByVal value As Integer)
            _codProveedor = value
        End Set
    End Property

    Private m_frmCompras As frmComprasGastos

    Public Property frmCompras() As frmComprasGastos
        Get
            Return m_frmCompras
        End Get
        Set(ByVal value As frmComprasGastos)
            m_frmCompras = value
        End Set
    End Property

    Private Sub CargarCompras()

        Dim dt As New DataTable
        dt = controller.get_Compras_All(oiml.dbConn, 0, fechaInicio.Value, fechaFin.Value, cboTipo.SelectedValue, _
                                    txtProveedor.Text, cboEstado.SelectedValue, txtNroComprobante.Text, "", codProveedor, False, "SI")
        dgCompras.DataSource = dt

    End Sub

    Private Sub CargarVentas()

        Dim dt As New DataTable
        dt = controllerVta.get_Ventas_All(oiml.dbConn, 0, fechaInicio.Value, fechaFin.Value, cboTipo.SelectedValue, _
                                    txtProveedor.Text, cboEstado.SelectedValue, txtNroComprobante.Text, "", codProveedor, False, "SI")
        dgCompras.DataSource = dt

    End Sub

    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        Me.Close()

    End Sub

    Private Sub btnEditar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEditar.Click
        Try
            If dgCompras.SelectedRows.Count > 0 Then

                Dim cod As New Integer
                cod = dgCompras.SelectedRows(0).Cells(0).Value

                If frmCompras Is Nothing Then

                    frmCompras = New frmComprasGastos
                    frmCompras.codCompra = cod
                    frmCompras.codTipoParam = cboTipo.Text
                    'If frmClientes.ShowDialog() = Windows.Forms.DialogResult.OK Then
                    frmCompras.ShowDialog()

                    CargarGrilla()

                    frmCompras.Dispose()
                    frmCompras = Nothing
                    'End If
                Else
                    frmCompras.codCompra = cod
                    frmCompras.codTipoParam = cboTipo.Text
                    frmCompras.ShowDialog()
                End If

            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Sub txtBuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBuscar.Click
        CargarGrilla()
    End Sub

    Private Sub txtProveedor_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtNroComprobante.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarCompras()

        End If
    End Sub

    Private Sub dgCompras_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgCompras.DoubleClick
        btnEditar_Click(sender, e)
    End Sub

    Private Sub dgCompras_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dgCompras.KeyDown
        If e.KeyCode = Keys.Enter Then
            btnEditar_Click(sender, e)
        End If

    End Sub

    Private Sub btnNuevo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNuevo.Click
        frmCompras = New frmComprasGastos()
        frmCompras.codCompra = 0
        frmCompras.codTipoParam = cboTipo.Text
        frmCompras.ShowDialog()
        CargarGrilla()
    End Sub

    Private m_frmBusProveedor As frmProveedorBuscador
    Public Property frmBusProveedor() As frmProveedorBuscador
        Get
            Return m_frmBusProveedor
        End Get
        Set(ByVal value As frmProveedorBuscador)
            m_frmBusProveedor = value
        End Set
    End Property

    Private Sub btnBuscProveedor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscProveedor.Click
        frmBusProveedor = New frmProveedorBuscador
        frmBusProveedor.ShowDialog()
        txtProveedor.Text = frmBusProveedor.ProveedorDesc
        codProveedor = frmBusProveedor.codProveedor
        If codProveedor > 0 Then
            CargarCompras()
        End If
    End Sub

    Private Sub btnCancelProveedor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelProveedor.Click
        codProveedor = 0
        txtProveedor.Text = ""
        CargarCompras()
    End Sub

    Private m_frmReporte As frmReportes
    Public Property frmRpts() As frmReportes
        Get
            Return m_frmReporte
        End Get
        Set(ByVal value As frmReportes)
            m_frmReporte = value
        End Set
    End Property

    Private Sub btnImprimir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImprimir.Click

        'If frmRpts Is Nothing Then
        '    frmRpts = New frmReportes
        '    'frmRpts.MdiParent = Me
        '    frmRpts.tipo_Reporte = "COMPRAS"
        '    frmRpts.datos_Reporte = CType(dgCompras.DataSource, DataTable).Copy
        '    frmRpts.ShowDialog()
        'Else

        '    If Not frmRpts.Visible Then
        '        frmRpts = New frmReportes()
        '        'frmRpts.MdiParent = Me
        '        frmRpts.tipo_Reporte = "COMPRAS"
        '        frmRpts.datos_Reporte = CType(dgCompras.DataSource, DataTable).Copy
        '        frmRpts.ShowDialog()
        '    Else
        '        frmRpts.tipo_Reporte = "COMPRAS"
        '        frmRpts.datos_Reporte = CType(dgCompras.DataSource, DataTable).Copy
        '        frmRpts.Select()
        '        frmRpts.Activate()
        '    End If

        'End If


    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelCliente.Click
        codCliente = 0
        txtCliente.Text = ""
        CargarVentas()
    End Sub

    Private m_frmBusCliente As frmClienteBuscador
    Public Property frmBusCliente() As frmClienteBuscador
        Get
            Return m_frmBusCliente
        End Get
        Set(ByVal value As frmClienteBuscador)
            m_frmBusCliente = value
        End Set
    End Property

    Private Sub btnBuscCliente_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscCliente.Click
        codCliente = 0
        frmBusCliente = New frmClienteBuscador
        frmBusCliente.ShowDialog()
        txtCliente.Text = frmBusCliente.ClienteDesc
        codCliente = frmBusCliente.codCliente

        If codCliente > 0 Then
            CargarVentas()
        End If

    End Sub

    Private Sub cboTipo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboTipo.SelectedIndexChanged
        CargarGrilla()
    End Sub

    Private Sub CargarGrilla()
        If cboTipo.Text.Contains("EGRESOS") Then
            CargarCompras()
        Else
            CargarVentas()
        End If

    End Sub

    Private Sub txtCliente_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtCliente.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarGrilla()
        End If

    End Sub
End Class